package com.easpeed.device.utils

import android.annotation.SuppressLint
import android.app.Activity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.blankj.utilcode.util.SizeUtils
import com.easpeed.comon.utils.SocketHelper
import com.easpeed.comon.utils.isUser
import com.easpeed.elevator.R

/**
 * Created by bigyu on 2024/11/6 19:11
 * com.easpeed.device.utils
 */
object UserConnectUiHelper {

    @SuppressLint("SetTextI18n")
    @JvmStatic
    fun addTips(mContext: Activity?) {
        if (isUser()) {
            return
        }
        if (mContext == null) {
            return
        }
        if (getView(mContext) != null) {
            return
        }
        val view = LayoutInflater.from(mContext).inflate(R.layout.item_connect_tips, null, false)
        view.findViewById<TextView>(R.id.name).text = "用户设备${SocketHelper.userName}已连接"
        view.tag = "socket_tips"
        val params = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, SizeUtils.dp2px(100f))
        (mContext.window.decorView as ViewGroup).addView(view, params)
    }

    @JvmStatic
    fun removeTips(mContext: Activity?) {
        if (isUser()) {
            return
        }
        if (mContext == null) {
            return
        }
        val view = getView(mContext)
        if (view != null) {
            (mContext.window.decorView as ViewGroup).apply {
                this.removeView(view)
            }
        }
    }


    fun getView(mContext: Activity?): View? {
        if (mContext == null) {
            return null
        }
        return (mContext.window.decorView as ViewGroup).findViewWithTag<View>("socket_tips")
    }
}